/**
 * 
 */
package com.suixun.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jsoup.helper.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.google.gson.Gson;
import com.suixun.entity.PackageInfo;
import com.suixun.entity.form.PackageForm;
import com.suixun.entity.page.PageModel;
import com.suixun.service.PackageService;
import com.suixun.util.Constant;

/**
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/packageManage")
public class PackageManage extends BaseManager {
	@Resource
	private PackageService packageService;

	/**
	 * 查询所有的套餐列表，不鉴权
	 * 
	 * @return
	 */

	@RequestMapping(value = "noCheck_queryPackage.json")
	@ResponseBody
	public Object queryPackageList(String param) {
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			PackageForm paramForm = new Gson().fromJson(param, PackageForm.class);
			return success(packageService.queryPackagelist(paramForm));
		} catch (Exception e) {
			logger("PackageManage queryPackageList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 分页查询套餐列表
	 * 
	 * @return
	 */

	@RequestMapping(value = "queryPackageList.json")
	@ResponseBody
	public Object queryPackageListPage(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			PackageForm paramForm = new Gson().fromJson(param, PackageForm.class);
			return success(packageService.queryPackagelistPage(paramForm));
		} catch (Exception e) {
			logger("PackageManage queryPackageListPage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 新增套餐
	 * 
	 * @return
	 */

	@RequestMapping(value = "addPackage")
	@ResponseBody
	public Object addPackage(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			PackageInfo packageinfo = new Gson().fromJson(param, PackageInfo.class);
			if (packageinfo == null || StringUtil.isBlank(packageinfo.getPackagename())) {
				return fail(Constant.EMPTY);
			}
			// 先判断套餐名称是否已存在
			PackageForm paramForm = new PackageForm();
			paramForm.setPackagename(packageinfo.getPackagename());
			PackageInfo packageInfo = packageService.queryPackageByName(paramForm);
			if (packageInfo != null && packageInfo.getId() != null)
				return fail("套餐已经存在");

			int res = packageService.addPackage(packageinfo, this.getUserId());

			if (res > 0) {
				return success(res);
			}else {
				return fail("新增失败");
			}
		} catch (Exception e) {
			logger("PackageManage addPackage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 修改套餐
	 * 
	 * @return
	 */

	@RequestMapping(value = "modifyPackage")
	@ResponseBody
	public Object modifyPackage(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			PackageInfo packageinfo = new Gson().fromJson(param, PackageInfo.class);
			if (packageinfo == null || StringUtil.isBlank(packageinfo.getPackagename()) || packageinfo.getId() == null) {
				return fail("必要参数为空");
			}
			// 先判断套餐名称是否已存在
			PackageForm paramForm = new PackageForm();
			paramForm.setPackagename(packageinfo.getPackagename());
			PackageInfo packageInfo = packageService.queryPackageByName(paramForm);
			if (packageInfo != null && packageInfo.getId() != null
					&& packageInfo.getId().intValue() != packageinfo.getId().intValue())
				return fail("套餐已经存在");

			int res = packageService.modifyPackage(packageinfo, this.getUserId());

			if (res > 0)
				return success(res);
			else
				return fail("修改失败");

		} catch (Exception e) {
			logger("PackageManage modifyPackage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 删除套餐
	 * 
	 * @return
	 */

	@RequestMapping(value = "deletePackage.json")
	@ResponseBody
	public Object deletePackage(String param) {
		boolean bln = checkRight();
		if (!bln) {
			return fail(Constant.Authority.CHECK_FALSE);
		}
		if (param == null) {
			return fail(Constant.EMPTY);
		}
		try {
			PackageForm paramForm = new Gson().fromJson(param, PackageForm.class);
			if (paramForm == null || StringUtil.isBlank(paramForm.getId())) {
				return fail("id为空");
			}
			PackageInfo packageInfo = packageService.queryPackageByID(paramForm);
			if (packageInfo == null) {
				return fail("套餐不存在");
			}
			int res = packageService.delPackage(packageInfo.getId());

			if (res > 0) {
				return success(res);
			}else {
				return fail("删除失败");
			}
		} catch (Exception e) {
			logger("PackageManage deletePackage:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}

	}

	/**
	 * 导出excel，不做鉴权
	 * 
	 * @return
	 */

	@RequestMapping(value = "export_PackageList")
	@ResponseBody
	public Object exportPackageList(String param) {
		if (param == null) {
			param = "";
		}
		try {
			PackageForm paramForm = new Gson().fromJson(param, PackageForm.class);
			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add("套餐名称");
			listParm.add("套餐详情");

			List<List<String>> listValue = new LinkedList<List<String>>();
			// 限制最多导出1万条数据，前端可以通过细化查询条件来多次导出
			if (StringUtil.isBlank(paramForm.getPagecount()))
				paramForm.setPagecount("10000");
			PageModel<PackageInfo> page = packageService.queryPackagelistPage(paramForm);
			List<PackageInfo> list = page.getQueryresult();

			for (int m = 0; m < list.size(); m++) {
				List<String> value = new LinkedList<String>();
				PackageInfo packageInfo = list.get(m);
				value.add(packageInfo.getPackagename());

				StringBuffer buffer = new StringBuffer();
				buffer.append("年费").append(packageInfo.getFee2());
				if (packageInfo.getFreetime() != null && packageInfo.getFreetime().intValue() != 0)
					buffer.append("、送").append(packageInfo.getFreetime()).append("个月");
				if (packageInfo.getTrytime() != null && packageInfo.getTrytime().intValue() != 0)
					buffer.append("、激活日起").append(packageInfo.getTrytime()).append("天免费");
				value.add(buffer.toString());
				listValue.add(value);
				packageInfo = null;
			}
			// 创建新的Excel 工作簿
			HSSFWorkbook workbook = exportDeviceList("套餐列表", listParm, listValue);
			HttpServletResponse response = this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=packagelist.xls");
			workbook.write(this.getResponse().getOutputStream());

			return success();
		} catch (Exception e) {
			logger("PackageManage exportPackageList:"+e.getMessage(),e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}
}
